package org.celery.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.celery.client.vo.WebInfoVo;
import org.celery.modules.member.entity.Member;
import org.celery.modules.other.entity.Pixel;
import org.celery.modules.other.entity.SevenElevenShop;
import org.celery.modules.other.enums.PixelStatusEnum;
import org.celery.modules.other.service.IPixelService;
import org.celery.modules.other.service.ISevenElevenShopService;
import org.celery.modules.other.vo.SevenElevenShopVO;
import org.celery.modules.other.wrapper.SevenElevenShopWrapper;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.Param;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.enums.UserStatusEnum;
import org.springblade.modules.system.service.IParamService;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("misc")
@Api(value = "杂项")
public class ClientMiscController {

    private final IParamService paramService;
    private final IPixelService pixelService;
    private final ISevenElevenShopService sevenElevenShopService;
    private final IUserService userService;
    private final IRoleService roleService;

    /**
     * 注册申请
     */
    @PostMapping("/register")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "注册申请")
    public R<Boolean> register(@Valid @RequestBody User user) {
        user.setPassword(DigestUtil.encrypt(user.getPassword()));
        user.setRealName(user.getName());

        User checkUser = userService.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getCode, user.getCode())
        );
        if (Func.isNotEmpty(checkUser) && checkUser.getStatus().equals(UserStatusEnum.NORMAL.getStatus())) {
            throw new ServiceException("工号已注册!");
        }
        if (Func.isNotEmpty(checkUser) && checkUser.getStatus().equals(UserStatusEnum.APPLYING.getStatus())) {
            throw new ServiceException("申请中，请勿重复提交!");
        }

        Role role = roleService.getOne(Wrappers.<Role>lambdaQuery()
                .eq(Role::getRoleAlias, "user")
        );
        if (Func.isEmpty(role)) {
            throw new ServiceException("后台配置错误, 请联系管理员");
        }

        user.setAccount(user.getCode());
        user.setRoleId(String.valueOf(role.getId()));
        user.setStatus(UserStatusEnum.APPLYING.getStatus());

        return R.status(userService.save(user));
    }


    /**
     * 网站信息
     */
    @GetMapping("/web-info")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "网站信息")
    public R<WebInfoVo> detail(String domain) {
        Param fbMsgIdParam = paramService.getOne(Wrappers.<Param>lambdaQuery()
                .eq(Param::getParamKey, "fb_msg_id")
        );
        WebInfoVo webInfo = new WebInfoVo();
        if (Func.isNotEmpty(fbMsgIdParam)) {
            webInfo.setFbMsgId(fbMsgIdParam.getParamValue());
        }

        List<Pixel> pixelList = pixelService.list(Wrappers.<Pixel>lambdaQuery()
                .eq(Pixel::getDomain, domain)
                .eq(Pixel::getStatus, PixelStatusEnum.NORMAL.getStatus())
        );
        webInfo.setPixelList(pixelList);

        return R.data(webInfo);
    }

    @PostMapping(value = "check-user-exist")
    public R<User> checkExist(@RequestParam String openId) {
        if (Func.isEmpty(openId)) {
            return R.data(null);
        }

        List<User> memberList = userService.list(Wrappers.<User>lambdaQuery()
                .eq(User::getOpenId, openId)
        );
        if (Func.isEmpty(memberList)) {
            return R.data(null);
        }

        return R.data(memberList.get(0));
    }

    @PostMapping(value = "unbind-wechat")
    public R<Boolean> unbindWechat() {
        BladeUser bladeUser = SecureUtil.getUser();
        if (Func.isNull(bladeUser)) {
            return R.data(true);
        }

        User user = userService.getById(bladeUser.getUserId());
        if (Func.isNull(user)) {
            return R.data(true);
        }

        user.setOpenId("");
        userService.updateById(user);

        return R.data(true);
    }

}
